
#include "../lib/curl/include/curl.h"
//#include "mk_util.h"
#include <stdio.h>
#include "iotv30_config.h"
#include "print_log.h"  
#include "curl_http.h"
#include "uart_printf.h"



#define  ota_log_debug logout

#define TRUE				1
#define FALSE				0

/*****************************************************************************/
size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
  	return fwrite(ptr, size, nmemb, stream);
} 



int curl_http_download(const char *url, const char *file_name, progress_cb p_cb)
{
	CURL *curl;
	CURLcode res = -1;
	FILE *outfile;
	char *progress_data = "*****### ";

    LOG_NAME();

	curl = curl_easy_init();
	if(curl)
	{
		outfile = fopen(file_name, "wb");
		if(outfile != NULL) {
		   ota_log_debug("download file:%s\n", file_name);
		 
		   curl_easy_setopt(curl, CURLOPT_URL, url);
		   curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
		   curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);
		   curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
		   curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, p_cb);////注册回调函数
		   curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, progress_data);////应该是通过这个传输参数

		   // 关闭证书
		   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
		   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);

		   res = curl_easy_perform(curl);
		   if(res) {
		   		log_err("curl download result:%d\n", res);
		   }
		   else {
		   		log_notice("curl download success!\n");
		   }
		 
		   fclose(outfile);
		}
		else {
			ota_log_debug("open file:%s err, download fail!\n", file_name);
		}
		/* always cleanup */
		curl_easy_cleanup(curl);
	}
	return res;

}

#if 0
static size_t read_callback(void *ptr, size_t size, size_t nmemb, FILE *stream) 
{
    return fread(ptr, size, nmemb, stream);
}

int curl_http_upload_simple(const char *filename, const char *url, progress_cb p_cb) 
{
    CURL *curl;
    CURLcode res;
    // 打开待上传的文件
    FILE *fp = fopen(filename, "rb");
    if (!fp) {
        printf("无法打开文件:%s\n", filename);
        return -1;
    }
    // 初始化libcurl
    curl_global_init(CURL_GLOBAL_ALL);
    // 创建CURL对象
    curl = curl_easy_init();
    if (curl) {
        // 设置上传的URL地址    :"http://example.com/upload"
        curl_easy_setopt(curl, CURLOPT_URL, url);
        // 设置读取回调函数和数据源
        curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
        curl_easy_setopt(curl, CURLOPT_READDATA, fp);
		curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, p_cb);
        // 启用断点续传模式
        curl_easy_setopt(curl, CURLOPT_RESUME_FROM_LARGE, ftell(fp));
        // 执行HTTP POST请求（使用PUT方法）
        res = curl_easy_perform(curl);
        // 检查是否发生错误
        if (res != CURLE_OK) {
            fprintf(stderr, "上传失败: %s\n", curl_easy_strerror(res));
        }
        // 清理资源
        curl_easy_cleanup(curl);
    }
    // 关闭文件句柄
    fclose(fp);
    // 清理libcurl全局资源
    curl_global_cleanup();
	
    return res;
}

#else
//#include <stdio.h>
//#include <curl/curl.h>

// 读取回调函数
size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream) {
    return fread(ptr, size, nmemb, (FILE *)stream);
}

int curl_http_upload_simple(const char *filename, const char *url, progress_cb p_cb) {
    CURL *curl;
    CURLcode res;

    // 打开待上传的文件
    FILE *fp = fopen(filename, "rb");
    if (!fp) {
        printf("无法打开文件:%s\n", filename);
        return -1;
    }

    // 初始化libcurl
    curl_global_init(CURL_GLOBAL_ALL);

    // 创建CURL对象
    curl = curl_easy_init();
    if (curl) {
        // 设置上传的URL地址
        curl_easy_setopt(curl, CURLOPT_URL, url);

        // 启用PUT方法
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
	    //curl_easy_setopt(curl, CURLOPT_POST, 1L); // 启用POST方法

        // 设置读取回调函数和数据源
        curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
        curl_easy_setopt(curl, CURLOPT_READDATA, fp);

        // 设置文件大小
        fseek(fp, 0, SEEK_END);
        long file_size = ftell(fp);
        fseek(fp, 0, SEEK_SET);
        curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_size);

        // 设置进度回调函数
        curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, p_cb);

        // 启用调试输出
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        // 执行HTTP请求
        res = curl_easy_perform(curl);

        // 检查是否发生错误
        if (res != CURLE_OK) {
            fprintf(stderr, "上传失败: %s\n", curl_easy_strerror(res));
        }

        // 清理资源
        curl_easy_cleanup(curl);
    }

    // 关闭文件句柄
    fclose(fp);

    // 清理libcurl全局资源
    curl_global_cleanup();

    return res;
}

#endif
